import socket
import struct

import json
from google.protobuf import json_format


def ip_to_int_using_socket(ip_str):  
    return int.from_bytes(socket.inet_aton(ip_str), byteorder='big')  

def int_to_ip(ip_int):  
    return ".".join(str(ip_int >> i & 0xFF) for i in (24, 16, 8, 0)) 

# 不怎么用
def ip_to_int_little_endian(ip_address):  
    # 将IP地址字符串分割成四个部分  
    parts = ip_address.split('.')  
    # 确保IP地址是有效的  
    if len(parts) != 4:  
        raise ValueError('Invalid IP address')  
    # 将每部分转换为整数，并使用struct模块打包为小端序的整数  
    packed_ip = struct.pack('<I', int(parts[0]), int(parts[1]), int(parts[2]), int(parts[3]))  
    # 将打包后的字节串转换为整数，并指定字节序为小端序  
    return int.from_bytes(packed_ip, byteorder='little')


# def toIP(num):
#     s = []
#     for i in range(4):
#         s.append(str(num %256))
#         num /= 256
#     return '.'.join(s[::-1])

# int_ip = 2886795267
# ip = socket.inet_ntoa(struct.pack('I',socket.htonl(int_ip)))
# print(ip)
# print(socket.gethostname())



# hostid=socket.gethostbyname(socket.gethostname())
# print(hostid) 
# print(ip_to_int_using_socket("172.17.0.3") )
print(int_to_ip(3638561035))
print("=====")

# with open('/home/lixiang/Downloads/topicdata.js', 'r') as file:
#     content = file.read()
#     print(content)


# json_str = json_format.MessageToJson(message)
# print(json_str)

